<?php
require_once BaseUtil::getModulePath("base")  .  '/services/BaseService.php';
require_once BaseUtil::getModulePath("systemlog")  .  '/models/EmailLogDao.php';
require_once BaseUtil::getModulePath("systemlog")  .  '/constants/LogConstant.php';
require_once BaseUtil::getModulePath("order")  .  '/models/OrderDao.php';
require_once BaseUtil::getModulePath("order")  .  '/models/OrderItemsDao.php';
require_once BaseUtil::getModulePath("customer")  .  '/models/CustomerDao.php';
require_once BaseUtil::getModulePath("component")  .  '/MailUtil.php';


class EmailLogService extends BaseService {
    private $emailLogDao;
    private $orderDao;
    private $orderItemDao;
    private $customerDao;

    public function  __construct() {
        parent::__construct();
        $this->emailLogDao=new EmailLogDao();
        $this->emailLogDao->setDb($this->db);
        $this->orderDao=new OrderDao();
        $this->orderDao->setDb($this->db);
        $this->orderItemDao=new OrderItemsDao();
        $this->orderItemDao->setDb($this->db);
        $this->customerDao=new CustomerDao();
        $this->customerDao->setDb($this->db);
    }
    
    public function getEmailLog($view,$post){
    	$emailType = LogConstant::getEmailTypeList();
    	
    	$dbGrid = new DBGrid($view);
    	$dbGrid->setPost($post);
    	$dbGrid->setModule('/systemlog/emaillog');
    	$dbGrid->addFetchTable('email_logs','*');
    	$dbGrid->addValueFilter('is_delete',0);
    	
    	$dbGrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT,'id',$this->_translate->translate("id"),DBGridConstant::OPERATOR_EQUAL);
    	$dbGrid->addOptionsFilter(DBGridConstant::FILTER_TYPE_SELECT, "email_type", $this->_translate->translate("emailType"),$emailType);
    	$dbGrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT,'send_to',$this->_translate->translate("sendTo"));
    	$dbGrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT,'send_cc',$this->_translate->translate("sendCc"));
    	$dbGrid->addDateFilter('created',$this->_translate->translate("sendDate"));
    	
    	$dbGrid->addFetchColumn('id',$this->_translate->translate("id"),"view");
    	$dbGrid->addFetchColumn('email_type',$this->_translate->translate("emailType"),'',$emailType);
    	$dbGrid->addFetchColumn('description',$this->_translate->translate("description"));
    	$dbGrid->addFetchColumn('created',$this->_translate->translate("sendDate"));
    	
    	$dbGrid->setPageTitle($this->_translate->translate("emailSendLog"));
		return $dbGrid->render();
    }
    
    
    public function getEmailLogById($id){
    	$emailLog = $this->emailLogDao->getEmailLogById($id);
    	$emailLog['send_to'] = explode(',',$emailLog['send_to']);
    	$emailLog['send_cc'] = explode(',',$emailLog['send_cc']);
    	return $emailLog;
    }
    
    public function deleteEmailLog($id){
        $this->emailLogDao->deleteEmailLog($id);
    }
    
}